package com.linsen.core.ohc.aspect;

import com.linsen.core.common.aspect.BaseAspect;
import com.linsen.core.common.constant.LinsenConstant;
import com.linsen.core.ohc.annotation.OHCacheRemove;
import com.linsen.core.ohc.util.OHCacheUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zhaoyong
 * @date 2023/4/6
 */
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class OHCacheRemoveAspect extends BaseAspect {
    private final OHCacheUtil<Object> ohCacheUtil;

    @Pointcut("@annotation(" + LinsenConstant.LINSEN_APP_PACKAGE + ".core.ohc.annotation.OHCacheRemove)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = point.proceed();
        OHCacheRemove ohCacheRemove = super.getAnnotation(point, OHCacheRemove.class);
        String key = ohCacheRemove.key();
        this.ohCacheUtil.del(key);
        return result;
    }
}
